package com.hotel.controller;

import com.hotel.pojo.Room;
import com.hotel.pojo.User;
import com.hotel.service.RoomService;
import com.hotel.service.impl.RoomServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;

    /**
     * 查询全部，返回到用户展示页面
     * @return
     */
    @RequestMapping("/all")
    public String getAll(Model model){
        List<Room> roomList = roomService.getAll();
        model.addAttribute("allRoom",roomList);
        return "allRoom";
    }

    /**
     * 跳转到增加用户页面
     */
    @RequestMapping("/toAddRoom")
    public String toAddRoom(){
        return "addRoom";
    }

    //添加用户的请求
    @RequestMapping("/addRoom")
    public String addRoom(Room room){
        roomService.addRoom(room);
        return "redirect:/room/all";//重定向
    }

    //跳转到修改页面
    @RequestMapping("/toUpdateRoom")
    public String toUpdateRoom(int id,Model model){
        Room room = roomService.selectRoomById(id);
        model.addAttribute("room",room);
        return "updateRoom";
    }

    //修改用户的请求
    @RequestMapping("/updateRoom")
    public String updateRoom(Room room){
        roomService.updateRoom(room);
        return "redirect:/room/all";//重定向
    }

    //删除用户的请求
    @RequestMapping("/deleteRoom/{id}")
    public String deleteRoom(@PathVariable("id") int id){
        roomService.deleteRoomById(id);
        return "redirect:/room/all";
    }

    //查询用户
    @RequestMapping("/selectRoom")
    public String selectRoom(String selectRoomName,Model model){
        List<Room> roomList = roomService.selectRoomByName(selectRoomName);

        if(roomList == null){
            roomList = roomService.getAll();
            model.addAttribute("error","未查到");
        }


        model.addAttribute("allRoom",roomList);
        return "allRoom";
    }
}
